#include "TcpServer.h"
TcpServer::TcpServer()
{
	m_fd = socket(AF_INET, SOCK_STREAM, 0);
}
TcpServer::~TcpServer()
{
	close(m_fd);
} 
int TcpServer::setListen(unsigned short port)
{
	struct sockaddr_in seraddr;
	seraddr.sin_family = AF_INET;
	seraddr.sin_port = htons(port);
	seraddr.sin_addr.s_addr = INADDR_ANY;
	int ret = bind(m_fd, (sockaddr*)&seraddr, sizeof(seraddr));
	if (ret == -1)
	{
		perror("bind");
		return -1;
	}
	listen(m_fd, 128);
	cout << "setListen success!" << endl;
}
TcpSocket* TcpServer::acceptConn(sockaddr_in *addr)
{
	struct sockaddr_in clint_addr;
	socklen_t addr_len = sizeof(clint_addr);
	int cfd= accept(m_fd, (struct sockaddr*)&clint_addr, &addr_len);
	if (cfd == -1)
	{
		perror("accept");
		return nullptr;
	}
	return new TcpSocket(cfd);
}